import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * http请求重定向
 */
public class S19_DownServletNew extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /*
        请求重定向细节
        1.最佳应用场景：网站迁移
        2.浏览器地址会发生变化，本质是两次http请求
        3.不能共享request域中的数据，本质是两次http请求，会产生两个httpServletRequest对象
        4.不能重定向到WEB-INF下的资源
        5.可以重定向到web工程意外的资源
        6.也可以通过设置response对象来完成重定向，如： response.setStatus(302);
                                                  response.setHeader("Location", "/servlet/xxx")
        7.动态获取application context
        getServletContext().getContextPath()
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/x-tar;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print("hello");
        out.flush();
        out.close();
    }
}
